// run: $exec < input
#include <iostream>
#include <queue>
#include <stack>

int const maxn = 107;
int a[maxn], b[maxn];
int n;

bool check_queue()
{
	std::queue<int> q;
	for (int i = 0; i < n; i++) q.push(a[i]);
	for (int j = 0; !q.empty() && b[j] == q.front(); j++) q.pop();
	return q.empty();
}

bool check_stack()
{
	std::stack<int> s;
	for (int i = 0; i < n; i++) s.push(a[i]);
	for (int j = 0; !s.empty() && b[j] == s.top(); j++) s.pop();
	return s.empty();
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::cin >> n;
		for (int i = 0; i < n; i++) std::cin >> a[i];
		for (int i = 0; i < n; i++) std::cin >> b[i];
		bool check_q = check_queue();
		bool check_s = check_stack();
		if (check_q && check_s)
			std::cout << "both\n";
		else if (check_q)
			std::cout << "queue\n";
		else if (check_s)
			std::cout << "stack\n";
		else
			std::cout << "neither\n";
	}
}

